package com.nova.android.tvlauncher.ui.fragment.atv

import android.content.pm.PackageInstaller
import androidx.core.view.isVisible
import com.nova.android.tvlauncher.AppCache
import com.nova.android.tvlauncher.databinding.FragmentUpdateAppEmtBinding
import com.nova.android.tvlauncher.ext.clickNoRepeatWithAnim
import com.nova.android.tvlauncher.ui.viewmodel.UpdateAppViewModel
import com.nova.lib_lang.R
import com.shudong.lib_base.ext.appContext
import com_github_lib_base.base.BaseVMFragment
import com_github_lib_base.ext.appVersionCode
import com_github_lib_base.ext.appVersionName
import com_github_lib_base.ext.jsonToBean
import com_github_lib_base.ext.stringValue
import com_github_lib_base.ext.stringValueWithFormat
import io.github.lib_autorun.bean.UpdateDto
import io.github.lib_autorun.task.manager.AppUpdateManager

class UpdateAppEmtFragment : BaseVMFragment<FragmentUpdateAppEmtBinding, UpdateAppViewModel>() {

    lateinit var updateDto: UpdateDto

    override fun initView() {

        updateDto = AppCache.updateInfo.jsonToBean()

        mBind.ivBigicon.setImageResource(com.nova.android.tvlauncher.R.mipmap.ic_launcher)
        mBind.tvDesc.text = R.string.update_text_03.stringValueWithFormat(
            appContext.appVersionName,
            appContext.appVersionCode,
            updateDto.version
        )

        mBind.tvUpdate.requestFocus()
    }

    override fun initClick() {
        mBind.tvUpdate.clickNoRepeatWithAnim {
            // 更新逻辑
            mBind.pbDownload.isIndeterminate = false
            mBind.tvProgress.text = "0 %"
            mBind.pbDownload.progress = 0
            mBind.tvCancle.isEnabled = false
            mBind.tvUpdate.isEnabled = false
            mBind.llProgress.isVisible = true
            mBind.tvTitle.isVisible = false
            mBind.tvDesc.isVisible = false
            AppUpdateManager.downloadApk(
                updateDto.downLink!!,
                { downloadProgress ->
                    mBind.pbDownload.isIndeterminate = false
                    mBind.tvProgress.text = "$downloadProgress %"
                    mBind.pbDownload.progress = downloadProgress.toInt()
                    mBind.tvCancle.isEnabled = false
                    mBind.tvUpdate.isEnabled = false
                    mBind.llProgress.isVisible = true
                    mBind.tvTitle.isVisible = false
                    mBind.tvDesc.isVisible = false
                }) { apkFile, error ->
                if (apkFile != null) {
                    //apk下载成功
                    mBind.tvCancle.isEnabled = false
                    mBind.tvUpdate.isEnabled = false
                    mBind.llProgress.isVisible = true
                    mBind.tvTitle.isVisible = false
                    mBind.tvDesc.isVisible = false
                    mBind.pbDownload.isIndeterminate = true
                    mBind.tvProgress.text = R.string.appdetail_text_07.stringValue()
                    AppUpdateManager.installApkOnly(apkFile) { installResult ->
                        when (installResult.status) {
                            PackageInstaller.STATUS_SUCCESS -> {
                                // 安装成功
                            }

                            PackageInstaller.STATUS_FAILURE_ABORTED -> {
                                // 安装被用户取消
                                mBind.tvCancle.isEnabled = true
                                mBind.tvUpdate.isEnabled = true
                                mBind.llProgress.isVisible = false
                                mBind.tvTitle.isVisible = true
                                mBind.tvDesc.isVisible = true
                                mBind.tvProgress.text = "0 %"
                                mBind.pbDownload.progress = 0
                            }

                            else -> {
                                // 安装失败，回退为等待状态
                                mBind.tvCancle.isEnabled = true
                                mBind.tvUpdate.isEnabled = true
                                mBind.llProgress.isVisible = false
                                mBind.tvTitle.isVisible = true
                                mBind.tvDesc.isVisible = true
                                mBind.tvProgress.text = "0 %"
                                mBind.pbDownload.progress = 0
                            }
                        }
                    }
                } else {
                    //APK下载失败
                    mBind.tvCancle.isEnabled = true
                    mBind.tvUpdate.isEnabled = true
                    mBind.llProgress.isVisible = false
                    mBind.tvTitle.isVisible = true
                    mBind.tvDesc.isVisible = true
                    mBind.tvProgress.text = "0 %"
                    mBind.pbDownload.progress = 0
                }
            }

        }
        mBind.tvCancle.clickNoRepeatWithAnim {
            //关闭升级页面
            requireActivity().finish()
        }
    }


}